//
//  TDCalendaAlertPickC.m
//  TBRentClient
//
//  Created by tonbright on 2018/6/11.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TDCalendaAlertPickC.h"
#import "YXDateHelpObject.h"

@interface TDCalendaAlertPickC ()<UIPickerViewDataSource,UIPickerViewDelegate>


@property (nonatomic,strong)NSMutableArray *hourArray;

@property (nonatomic,strong)NSMutableArray *minArray;

@property (nonatomic,copy)NSString *currentDateStr;

@end

@implementation TDCalendaAlertPickC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    
    NSMutableArray *tmpAllHourArray = [NSMutableArray array];
    for (int i = 9; i < 18; i++) {
        NSString *iStr = [NSString stringWithFormat:@"%02d",i];
        [tmpAllHourArray addObject:iStr];
    }
    _hourArray = tmpAllHourArray;
    _minArray = @[@"00",@"15",@"30",@"45"].mutableCopy;

//    [_dateArray removeObjectAtIndex:0];
//    self.timeDurationArray = @[@"3",@"5",@"7"].mutableCopy;

    _view_big.layer.borderColor = [UIColor colorWithHex:0xa9a9a9].CGColor;
    _view_big.layer.borderWidth = 1;
    NSInteger hourIndex = 0;
    NSInteger minIndex = 0;
    if (_isStart) {
        hourIndex = [AppSetting shareAppSetting].startCalendaHourIndex ? [AppSetting shareAppSetting].startCalendaHourIndex : 0;
        minIndex = [AppSetting shareAppSetting].startCalendaMinIndex ? [AppSetting shareAppSetting].startCalendaMinIndex : 0;
    }else {
        hourIndex = [AppSetting shareAppSetting].endCalendaHourIndex ? [AppSetting shareAppSetting].endCalendaHourIndex : 0;
        minIndex = [AppSetting shareAppSetting].endCalendaMinIndex ? [AppSetting shareAppSetting].endCalendaMinIndex : 0;
    }
    [self.pickerView selectRow:hourIndex inComponent:0 animated:NO];
    [self.pickerView selectRow:minIndex inComponent:1 animated:NO];

    

    self.selDateStr = [[YXDateHelpObject manager] getCurentTime:@"yyyy年MM月dd日" dateN:_selDate];
    self.currentWeek = [[YXDateHelpObject manager] getBaseWeekly:_selDate];
    [self setLbe_titleText];
}



- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.hourArray.count;
    }
    return self.minArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return Width / 2;
    }else {
        return Width / 2;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return self.hourArray[row];
    }else{
        return self.minArray[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self setLbe_titleText];
}


- (IBAction)cancleBtnClick:(UIButton *)sender {
    if (sender.tag == 10) {
        if (self.pickerViewCancleBtnBlock) {
            self.pickerViewCancleBtnBlock();
        }
    }else if (sender.tag == 20) {
        NSInteger hourIndex = [self.pickerView selectedRowInComponent:0];
        NSInteger minIndex = [self.pickerView selectedRowInComponent:1];
        NSLog(@"%ld-----%ld",hourIndex,minIndex);
        
        if (self.pickerViewSureBtnBlock) {
            //吧年月日保存起来吗?---------
            NSString *hourStr = self.hourArray[hourIndex];
            NSString *minStr = self.minArray[minIndex];
            NSString *hourMinStr = [NSString stringWithFormat:@"%@:%@",hourStr,minStr];
            self.pickerViewSureBtnBlock(hourStr,minStr,hourMinStr,hourIndex,minIndex);
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.pickerViewCancleBtnBlock) {
        self.pickerViewCancleBtnBlock();
    }
}


// MARK: - 设置顶部文本字体变化
- (void)setLbe_titleText {
    NSInteger dateIndex = [self.pickerView selectedRowInComponent:0];
    NSInteger hourIndex = [self.pickerView selectedRowInComponent:1];
    NSLog(@"%ld-----%ld",dateIndex,hourIndex);

        //吧年月日保存起来吗?---------
    NSString *hourStr = self.hourArray[dateIndex];
    NSString *minStr = self.minArray[hourIndex];
    
    _lbe_title.text = [NSString stringWithFormat:@"%@ %@:%@ %@",_selDateStr,hourStr,minStr,_currentWeek];
}


@end
